home *** CD-ROM | disk | FTP | other *** search
- /*
- GetTLE - writedb.c - Handling of PocketSat database
- Copyright ⌐2000 Andreas Schneider
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
- #include <PalmOS.h>
- #include "alerts.h"
- #include "Progress.h"
- #include "debug.h"
- #include "PSatDB.h"
- #include "GetTLERsc.h" // should use progress.c instead
-
- // structures and defines imposed by other programs
- typedef struct {
- char note; // actually a null terminated string
- } psatDBRecordType;
-
- typedef psatDBRecordType * psatDBRecordPtr;
-
- #define psatCreatorID 'pSat'
- #define psatDBName "GetTLE satellites"
- #define psatDBType 'iTLE'
-
- // globals
- DmOpenRef psatDB=0;
-
- extern void OpenPSatDB(void)
- {
- Err err;
- psatDB=DmOpenDatabaseByTypeCreator(psatDBType,psatCreatorID,dmModeReadWrite);
- if (psatDB)
- {
- // database exists and could be opened - no problems
- // LogMessage("Database found\n");
- }
- else
- {
- // maybe database just doesn't exist yet - create it
- LogMessage("Database not found");
- err=DmCreateDatabase(0,psatDBName,psatCreatorID,psatDBType,false);
- if (err)
- {
- LogMessage("failed to create database\n");
- }
- else
- {
- psatDB=DmOpenDatabaseByTypeCreator(psatDBType,psatCreatorID,dmModeReadWrite);
- if (psatDB)
- {
- // LogMessage("Database now found\n");
- }
- else
- {
- LogMessage("Database still not found.\n");
- }
- }
- }
- return;
- }
-
- extern void PSatDBAppendLine(Char *line)
- {
- UInt16 index=10000; // end of database
- MemHandle recordHandle;
- psatDBRecordPtr recordPtr;
- Err result;
- UInt32 length;
-
- // LogMessage(line);
- if (psatDB)
- {
- // allocate memory
- length=(UInt32)StrLen(line);
- recordHandle=(MemHandle)DmNewHandle(psatDB,(UInt32)(length+1)); // +1 - the trailin 0
- if (recordHandle!=NULL)
- {
- // copy string
- recordPtr=MemHandleLock(recordHandle);
- DmStrCopy(recordPtr,0,line);
- MemPtrUnlock(recordPtr);
- // append to database
- result=DmAttachRecord(psatDB,&index,recordHandle,0);
- if (result)
- {
- // release memory
- MemHandleFree(recordHandle);
- }
- }
- else
- {
- LogMessage("Can't get new handle");
- }
- }
- else
- {
- LogMessage("Can't find memo database");
- }
- return;
- }
-
- extern void ClearPSatDB(void)
- {
- UInt16 num_records;
- Char message[50]="";
- LocalID dbID;
- Err error;
-
- if (psatDB)
- {
- // how many records are there in the database
- num_records=DmNumRecordsInCategory(psatDB,dmAllCategories);
- // 3 lines are one TLE set - therefore:
- StrPrintF(message,"Delete %i TLE sets?",num_records / 3);
- if (MyConfirmFunc(message)==true)
- {
- // first close the database - can't delete it while it's open
- ClosePSatDB();
- // next get it's LocalId from the database name
- dbID=DmFindDatabase(0,psatDBName);
- if (dbID==0)
- {
- // DmFindDatabase returns 0 if database not found
- MyErrorFunc("Couldn't find database",psatDBName);
- return;
- }
- // try deleteing the database
- error=DmDeleteDatabase(0,dbID);
- if (error!=0)
- {
- // DmDeleteDatabase returns 0 on success
- MyErrorFunc("Couldn't delete Database",psatDBName);
- LogMessage("Delete DB error #%i\n",error);
- return;
- }
- MyStatusFunc("Database erased.");
- // next create a new, empty database
- OpenPSatDB();
- }
- }
- return;
- }
-
- // the usual cleanup
- extern void ClosePSatDB(void)
- {
- if (psatDB)
- {
- DmCloseDatabase(psatDB);
- }
- psatDB=NULL;
- return;
- }